package com.baidu.ocrgeneralocrdemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.ocrgeneralocrdemo.utils.ToastUtil;
import com.mengpeng.mphelper.ToastUtils;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.request.PostRequest;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;


import okhttp3.RequestBody;


public class CustomCameraActivity extends AppCompatActivity {
    public static final String KEY_IMAGE_PATH = "imagePath";
    private ImageView resultImg;
    private Button uploadBtn;
    Uri cropImageUri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_camera);
        resultImg = findViewById(R.id.result);
        uploadBtn = findViewById(R.id.camare_upload);
        uploadBtn.setVisibility(View.GONE);
    }
    public void gotoCamare(View view) {
        CameraActivity.startMe(this, 2005, CameraActivity.MongolianLayerType.BANK_CARD);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            cropImageUri = Uri.fromFile(new File(data.getStringExtra(KEY_IMAGE_PATH)));

            resultImg.setImageURI(cropImageUri);
            uploadBtn.setVisibility(View.VISIBLE);
        }
    }

    public void gotoUpLoad(View view) {


        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(cropImageUri));
            PredictorWrapper.asyncTestOneImage(this, bitmap, resultImg, null, new PredictorResultListener() {
                @Override
                public void ResultListener(String result) throws IOException {
                postResultToService(result);
                  //  Context context;
                   // ToastUtil.showToast(context,result);
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//        CameraActivity.startMe(this, 2005, CameraActivity.MongolianLayerType.BANK_CARD);
    }

    public static void postResultToService(String result) throws IOException {


        final MediaType contentType = MediaType.parse("application/json; charset=utf-8");
        Map<String, String> map = new HashMap<String, String>();
        map.put("parameter",result);
        JSONObject jsonObject = new JSONObject(map);
        RequestBody requestBody = RequestBody.create(contentType, jsonObject.toString());
       EasyHttp.post("/orc/parsingInformation")
               .baseUrl("http://192.168.0.115:8999")
                .requestBody(requestBody)
                 .execute(new CallBack<String>() {
                     @Override
                     public void onStart() {

                     }

                     @Override
                     public void onCompleted() {

                     }

                     @Override
            public void onSuccess(String s) {

                         ToastUtils.onDefaultShowToast("成功");
            }

            @Override
            public void onError(ApiException e) {

            }
        });


    }
}